<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2019/4/12
 * Time: 15:39
 */

namespace app\admin\controller;


use think\Db;

class Project extends Base
{
    public $table = 'project';

    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
        $keyword = input('keyword');
        $status = input('status/d');
        $server_id = input('server_id/d');
        if($keyword)
            $where['a.name'] = ['like','%'.$keyword.'%'];
        if($status)
            $where['a.status'] = ['eq',$status];
        if($server_id)
            $where['a.server_id'] = ['eq',$server_id];
        if(!isset($where))
            $where = '1=1';
        $data['list'] = Db::name($this->table)
            ->alias('a')
            ->join('attr b','b.id=a.server_id','left')
            ->where($where)
            ->order('a.id desc')
            ->field('a.*,b.name as server_name')
            ->paginate();
        $data['page'] = $data['list']->render();

        $this->assign('server',$this->getServer());
        $this->assign('data',$data);
        $this->assign('status',$status);
        $this->assign('server_id',$server_id);
        $this->assign('keyword',$keyword);
        return $this->fetch();
    }

    public function getServer()
    {
        $server = Db::name('attr')->where('type','server')->order('sort desc,id desc')->select();
        return $server;
    }

    public function add()
    {
        if(request()->isPost()){
            return $this->save();
        }
        $this->assign('server',json_encode($this->getServer()));
        $this->assign('data',1);
        $this->assign('title','新增');
        $this->assign('url',url('add'));
        return $this->fetch('info');
    }

    public function update()
    {
        $id = input('id/d');
        $data = Db::name($this->table)->where('id',$id)->find();
        if(!$data){
            $this->error('不存在');
        }
        if(request()->isPost()){
            return $this->save();
        }
        $this->assign('server',json_encode($this->getServer()));

        $data['start'] = $data['start']?date('Y-m-d',$data['start']):'';
        $data['end'] = $data['end']?date('Y-m-d',$data['end']):'';
        $this->assign('data',json_encode($data));
        $this->assign('title','编辑');
        $this->assign('url',url('update'));
        return $this->fetch('info');
    }

    public function save()
    {
        $id = input('id/d');
        $param = input();
        $data = [
            'name'=>input('name'),
            'remark'=>input('remark'),
            'server_id'=>input('server_id/d'),
            'price'=>input('price/d'),
            'start'=>empty($param['start'])?0:strtotime(input('start')),
            'end'=>empty($param['end'])?0:strtotime(input('end')),
            'info'=>input('info'),
            'status'=>input('status/d'),
            'modtime'=>time(),
        ];

        if($id){
            $a = Db::name($this->table)->where('id',$id)->update($data);
        }else{
            $data['addtime'] = time();

            $a = Db::name($this->table)->insert($data);
        }
        if($a){
            echo api_return(1,'保存成功');
        }else{
            echo api_return(0,'保存失败');
        }
        return;
    }

    public function ajaxupdate()
    {
        $type = input('type');
        $field = input('field');
        $data[$field] = input('value');
        $id = input('id/d');
        $data['modtime'] = time();
        $g = Db::name($this->table)->where('id',$id)->find();
        if(!$g)
            return dataReturn(0,'不存在');

        $a = Db::name($this->table)->where('id',$id)->update($data);
        if($a){
            echo api_return(1,'保存成功');
        }else{
            echo api_return(0,'保存失败');
        }
    }
}